package com.jic.point.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.jic.common.base.vo.RestResult;
import com.jic.point.constant.consist.ResponseCode;
import com.jic.point.exception.AppRuntimeException;
import com.jic.point.model.entity.ConfSupplier;
import com.jic.point.model.entity.ConfSupplierApply;
import com.jic.point.model.vo.request.ConfSupplierRequest;
import com.jic.point.model.vo.response.ConfSupplierResponse;
import com.jic.point.service.ConfSupplierApplyService;
import com.jic.point.service.ConfSupplierService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
/**
 * @Author: tzc
 * @date: 2020-10-11 17:28:33
 */
 @RestController
 @Slf4j
 @RequestMapping("/confSupplier")
 @Api(tags = " [供应商主表]控制类-PC")
public class ConfSupplierController {
	@Autowired
	private ConfSupplierService service;
	@Resource
    ConfSupplierApplyService confSupplierApplyService;
	
     /**
      *新增记录
      * @param request 新增[供应商主表]请求参数
      * @return ConfSupplierResponse 新增[供应商主表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
    @ApiOperation(value = "新增[供应商主表]记录")
    @PostMapping("/save")
    @ResponseBody
    public RestResult<Long> save(@RequestBody ConfSupplierRequest request) throws Throwable {
     		ConfSupplier confSupplier = new ConfSupplier();
    		BeanUtils.copyProperties(request, confSupplier);
    	    long flag = service.save(confSupplier);
     return RestResult.success(flag);
    }
	
     /**
      * 新增或修改记录
      * @param request 新增或修改[供应商主表]请求参数
      * @return ConfSupplierResponse 新增或修改[供应商主表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "保存或更新[供应商主表]")
     @PostMapping("/saveOrUpdate")
      @ResponseBody
     public RestResult<Long> saveOrUpdate(@Validated @RequestBody ConfSupplierRequest request) throws Throwable {
      		ConfSupplier confSupplier = new ConfSupplier();
		     BeanUtils.copyProperties(request, confSupplier);
	         long flag =   service.saveOrUpdate(confSupplier);
      return RestResult.success(flag);
     }
	
     /**
      * 删除记录
      * @param request 删除[供应商主表]请求参数
      * @return ConfSupplierResponse 删除[供应商主表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "删除[供应商主表]记录")
     @PostMapping("/delete")
     @ResponseBody
     public RestResult<Long> delete(@RequestBody ConfSupplierRequest request) throws Throwable {
      		ConfSupplier confSupplier = new ConfSupplier();
	     	BeanUtils.copyProperties(request, confSupplier);
	     	ConfSupplier result = service.getByPrimaryKey(confSupplier);
	     	//主表逻辑删除
            service.deleteByPrimaryKey(confSupplier);
             //操作人
             result.setAuditorId(request.getAdminOperate().getOperator());
             //设置当前动作
             result.setCurrentAction(2);
             //插入到申请表中
             ConfSupplierApply confSupplierApply = new ConfSupplierApply();
             BeanUtils.copyProperties(result, confSupplierApply);

         long flag = confSupplierApplyService.insertSelective(confSupplierApply);

      return RestResult.success(flag);
     }
	
     /**
      * 修改记录
      * @param request 修改[供应商主表]请求参数
      * @return ConfSupplierResponse 修改[供应商主表]响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
     @ApiOperation(value = "修改[供应商主表]记录")
     @PostMapping("/update")
     @ResponseBody
     public RestResult<Long> update(@RequestBody ConfSupplierRequest request) throws Throwable {
      		ConfSupplier confSupplier = new ConfSupplier();
	     	BeanUtils.copyProperties(request, confSupplier);
	         long flag = service.updateByPrimaryKeySelective(confSupplier);
      return RestResult.success(flag);
     }
	
     /**
      * 单表查询详情信息
      * @param request 查询[供应商主表]详情请求参数
      * @return ConfSupplierResponse 查询[供应商主表]详情响应参数
      * author tzc
      * @date 2020-3-11 20:54:04
      */
      @PostMapping("/getByPrimaryKey")
      @ApiOperation(value = "查询[供应商主表]详情")
      @ResponseBody
     public RestResult<ConfSupplierResponse> getByPrimaryKey(@Validated  @RequestBody ConfSupplierRequest request) {
     	log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
	     if (request!=null && request.getId()!=null) {
	      
	     	ConfSupplier confSupplier = new ConfSupplier();
	     	BeanUtils.copyProperties(request, confSupplier);
	     	ConfSupplier result = service.getByPrimaryKey(confSupplier);
		     log.info("查询详情请求参数出参，{}", JSONObject.toJSONString(result));
		     if (result == null) {
		     	return RestResult.error(ResponseCode.EXCEPTION, "记录不存在");
		     }
		     ConfSupplierResponse bo = new ConfSupplierResponse();
		     BeanUtils.copyProperties(result, bo);
	     	return RestResult.success(bo);
	     }
     	log.info("查询详情请求结束");
	     return RestResult.success(new ConfSupplierResponse());
     }
	
    /**
      * 分页查询
      *
      * @param request 分页查询请求参数
      * @return 分页列表
      * @throws Throwable 业务异常和系统异常
      */
     @PostMapping("/listPage")
     @ApiOperation(value = " 分页查询列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfSupplier>> listPage (@RequestBody ConfSupplierRequest request) throws Throwable {
      ConfSupplier param = new ConfSupplier();
      BeanUtils.copyProperties(request, param);
      param.setDeleteFlag(0);
     return RestResult.success(service.listPageBySelective(param, request.getPageNum(),request.getPageSize()));
     }

    /**
     * 查询所以的冻结状态
     * @param request
     * @return
     * @throws Throwable
     */
     @PostMapping("/listPagefreeze")
     @ApiOperation(value = " 分页查询冻结列表 ")
     @ResponseBody
     public RestResult<PageInfo<ConfSupplier>> listPagefreeze (@RequestBody ConfSupplierRequest request) throws Throwable {
      ConfSupplier param = new ConfSupplier();
      BeanUtils.copyProperties(request, param);
     return RestResult.success(service.listPageBySelectivefreeze(param, request.getPageNum(),request.getPageSize()));
     }
     
	
      /**
       *CONF_SUPPLIER 查询列表（不分页）
       * @param  request 前端请求参数
       */
      @PostMapping("/listAll")
      @ApiOperation(value = "不分页查询 [供应商主表] ")
      @ResponseBody
      public RestResult<List<ConfSupplierResponse>>  listConfSupplierAllRecord(@RequestBody ConfSupplierRequest request) throws Throwable {
      		ConfSupplier param = new ConfSupplier();
	     	BeanUtils.copyProperties(request, param);
	     	param.setDeleteFlag(0);
	     	List<ConfSupplier> list = service.listAllRecord(param);
			List<ConfSupplierResponse> responseList = list.stream().map(temp -> {
			ConfSupplierResponse bo = new ConfSupplierResponse();
			BeanUtils.copyProperties(temp, bo);
			return bo;
			}).filter(item -> item != null).collect(Collectors.toList());
		    return RestResult.success(responseList);
      }
    @PostMapping("/queryName")
    @ApiOperation(value = "不分页查询 [供应商主表] ")
    @ResponseBody
    public RestResult<List<ConfSupplierResponse>>  listConfSupplierQueryName(@RequestBody ConfSupplierRequest request) throws Throwable {
        ConfSupplier param = new ConfSupplier();
        BeanUtils.copyProperties(request, param);
        param.setDeleteFlag(0);
        List<ConfSupplier> list = service.listAllRecord(param);
        List<ConfSupplierResponse> responseList = list.stream().map(temp -> {
            ConfSupplierResponse bo = new ConfSupplierResponse();
            BeanUtils.copyProperties(temp, bo);
            return bo;
        }).filter(item -> item != null).collect(Collectors.toList());
        return RestResult.success(responseList);
    }

    /**
     * 冻结申请&&解冻申请
     */
    @PostMapping("/freezeApplication")
    @ResponseBody
    public RestResult<Long> freezeApplication(@Validated @RequestBody ConfSupplierRequest request) throws Throwable {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        ConfSupplier param = new ConfSupplier();
        BeanUtils.copyProperties(request, param);
        ConfSupplier result = service.getByPrimaryKey(param);
        //主表逻辑删除为1
        service.deleteByPrimaryKeyLogically(param);
        //赋值
        result.setSts(request.getSts());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfSupplierApply supplierApply = new ConfSupplierApply();
        BeanUtils.copyProperties(result, supplierApply);
        supplierApply.setCurrentAction(1);
        supplierApply.setAuditSts(0);
        long flag = confSupplierApplyService.insertSelective(supplierApply);
        return RestResult.success(flag);
    }
    /**
     * 延期提交
     */
    @PostMapping("/delayApply")
    @ResponseBody
    public RestResult<Long> delayApply(@Validated @RequestBody ConfSupplierRequest request) throws Throwable {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        ConfSupplier param = new ConfSupplier();
        BeanUtils.copyProperties(request, param);
        ConfSupplier result = service.getByPrimaryKey(param);
        if(result.getEndTime().getTime() >= request.getEndTime().getTime()){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "延期时间需大于活动结束时间");
        }
        //主表逻辑删除为1
        service.deleteByPrimaryKeyLogically(param);
        //赋值
        result.setSts(request.getSts());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfSupplierApply supplierApply = new ConfSupplierApply();
        BeanUtils.copyProperties(result, supplierApply);
        supplierApply.setCurrentAction(1);
        supplierApply.setAuditSts(0);
        long flag = confSupplierApplyService.insertSelective(supplierApply);
        return RestResult.success(flag);
    }
    /**
     * 提前提交
     */
    @PostMapping("/advanceApply")
    @ResponseBody
    public RestResult<Long> advanceApply(@Validated @RequestBody ConfSupplierRequest request) throws Throwable {
        log.info("查询详情请求参数入参，{}", JSONObject.toJSONString(request));
        ConfSupplier param = new ConfSupplier();
        BeanUtils.copyProperties(request, param);
        ConfSupplier result = service.getByPrimaryKey(param);
        if(result.getEndTime().getTime() <= request.getEndTime().getTime()){
            throw new AppRuntimeException(ResponseCode.EXCEPTION, "提前时间需小于活动结束时间");
        }
        //主表逻辑删除为1
        service.deleteByPrimaryKeyLogically(param);
        //赋值
        result.setSts(request.getSts());
        result.setApplyReason(request.getApplyReason());
        result.setApplierId(request.getAdminOperate().getOperator());
        //插入到申请表
        ConfSupplierApply supplierApply = new ConfSupplierApply();
        BeanUtils.copyProperties(result, supplierApply);
        supplierApply.setCurrentAction(1);
        supplierApply.setAuditSts(0);
        long flag = confSupplierApplyService.insertSelective(supplierApply);
        return RestResult.success(flag);

    }
     }

	
